package com.hugo.maomi_first.home.model;

import com.hugo.maomi_first.Interface.OnLoadHomeGoodsListener;
import com.hugo.maomi_first.Interface.OnLoadHomeHeaderListener;
import com.hugo.maomi_first.Interface.ServiceInterface;
import com.hugo.maomi_first.bean.HomeGoodsBean;
import com.hugo.maomi_first.bean.HomeHeaderBean;
import com.hugo.maomi_first.utils.UrlConstant;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/6/27.
 */
public class HomeModelImpl implements IHomeModel {

    private static final String TAG = "HomeModelImpl";

    //加载HomeHeader
    @Override
    public void loadHomeHeader(final OnLoadHomeHeaderListener listener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstant.URL_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<HomeHeaderBean> call = retrofit.create(ServiceInterface.class).getHomeHeader();
        call.enqueue(new Callback<HomeHeaderBean>() {
            @Override
            public void onResponse(Call<HomeHeaderBean> call, Response<HomeHeaderBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    HomeHeaderBean bean = response.body();
                    HomeHeaderBean.DataBean data = bean.getData();
                    listener.onSuccess(data);
                }
            }

            @Override
            public void onFailure(Call<HomeHeaderBean> call, Throwable t) {

            }
        });
    }

    //加载商品栏目
    @Override
    public void loadHomeGoods(OnLoadHomeGoodsListener listener) {
        callNetwork(listener);
    }

    private void callNetwork(final OnLoadHomeGoodsListener listener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstant.URL_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<HomeGoodsBean> call = retrofit.create(ServiceInterface.class).getHomeGoods();
        call.enqueue(new Callback<HomeGoodsBean>() {
            @Override
            public void onResponse(Call<HomeGoodsBean> call, Response<HomeGoodsBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    HomeGoodsBean goodsBean = response.body();
                    listener.onSuccess(goodsBean);
                }
            }

            @Override
            public void onFailure(Call<HomeGoodsBean> call, Throwable t) {

            }
        });
    }



}
